package com.shanjupay.merchant.service.impl;

import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.common.util.QiniuUtils;
import com.shanjupay.merchant.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class FileServiceImpl implements FileService {


    @Value("${oss.qiniu.accessKey}")
    private String accessKey ;

    @Value("${oss.qiniu.secretKey}")
    private String secretKey;

    @Value("${oss.qiniu.bucket}")
    private String bucket;

    @Value("${oss.qiniu.url}")
    private String url;

    /**
     * 上传文件
     *
     * @param bytes    文件字节数组
     * @param fileName 文件名称过
     * @return
     * @throws BusinessException
     */
    @Override
    public String uploadFile(byte[] bytes, String fileName) throws BusinessException {
        try {
            // 调用Common 下的工具 文件上传工具类
            String qiniuResult = QiniuUtils.upload2Qiniu(accessKey, secretKey, bucket, bytes, fileName);
        }catch (Exception e){
            log.error("上传文件出错了! {}",e.getMessage());
            throw new BusinessException(CommonErrorCode.E_100106);
        }
        return url+"/"+fileName;
    }
}
